package com.sgb.kjwl.view.ui.adapter.packers;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.SelfEmploymentEditEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
*@author 陈方凯
*@date 2021/5/31
*description:
*/
public class NewTextAdapter extends BaseRecycleAdapter<SelfEmploymentEditEntity.NewText> {

    public IClose iClose;


    public NewTextAdapter(Context context, List<SelfEmploymentEditEntity.NewText> data) {
        super(context, R.layout.item_new_text, data);
    }

    @Override
    public void convert(ItemViewHolder holder, SelfEmploymentEditEntity.NewText data) {
        holder.setBinding(BR.viewModel, data);
        EditText name = holder.getItem().findViewById(R.id.et_name);
        EditText content = holder.getItem().findViewById(R.id.et_content);
        ImageView imgClose = holder.getItem().findViewById(R.id.img_close);
        name.setText(data.getName());
        content.setText(data.getContent());
        imgClose.setOnClickListener(v -> {
            iClose.close(holder.getPosition());
        });
        content.setOnTouchListener(onTouchListener);
    }

    public interface IClose {
        void close(int position);
    }

    public void setOnClose(IClose iClose) {
        this.iClose = iClose;
    }

    public void upDate() {
        notifyDataSetChanged();
    }



    private View.OnTouchListener onTouchListener = (v, event) -> {
        if (MotionEvent.ACTION_DOWN == event.getAction()) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
        } else if (MotionEvent.ACTION_UP == event.getAction()) {
            v.getParent().requestDisallowInterceptTouchEvent(false);
        }
        return false;
    };

}
